package com.hk.modules.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hk.common.utils.StringUtils;
import com.hk.modules.user.mapper.UserMapper;
import com.hk.modules.user.pojo.UserPojo;
import com.hk.modules.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper,UserPojo> implements UserService {
    @Autowired
    private UserMapper userMapper;



    @Override
    public UserPojo queryUserByAccount(UserPojo userPojo) {
        return userMapper.queryUserByAccount(userPojo);
//        if("admin".equals(userPojo.getUsername())){
//            userPojo.setNickName("管理员");
//            userPojo.setPassword("379873c3878ccaecd2ce740f41f25bba");
//            return userPojo;
//        }else{
//            return null;
//        }
    }

    @Override
    public IPage<UserPojo> selectPage(IPage page, UserPojo userPojo) {
        return userMapper.selectPage(page,new QueryWrapper<>(userPojo));
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int  saveUserAndRole(UserPojo userPojo) {
        int result = baseMapper.insert(userPojo);
        userMapper.insertUserRole(userPojo);
        return result;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int updateUserAndRole(UserPojo userPojo) {
        int restlt = baseMapper.updateById(userPojo);
        if(!StringUtils.isEmpty(userPojo.getRoleId())){
            userMapper.updateUserRole(userPojo);
        }
        return restlt;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int deleteUserRole(UserPojo userPojo) {
       int re= baseMapper.deleteById(userPojo);
       userMapper.deleteUserRole(userPojo);
       return re;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public UserPojo selectUserRole(UserPojo userPojo) {
        return baseMapper.selectById(userPojo);
    }


}
